package com.sdp.core.bizc.utils;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.IService;
import com.sdp.core.bizc.db.QueryUtil;
import com.sdp.core.bizc.exception.ApplicationException;
import com.sdp.core.bizc.vo.Result;

@Component
public class UniqueCheckTools {

	/**
	 * 添加和更新分开验证，不会同时验证添加和更新数据，并且不能多记录同时验证
	 * @param checkObj 验证对象
	 * @param checkFields 验证字段,实体字段
	 * @param insertObj 添加的验证数据
	 * @param updateObj 修改的验证数据
	 * @param isjoint 是否联合唯一
	 * @return
	 */
	public <T> Result<?> isUniqueSingle(IService<T> service,Class<T> checkObj,String[] checkFields,String pk,Object insertObj,Object updateObj,boolean isjoint) {
		
		if(checkFields==null || checkFields.length==0) {
			return Result.error("校验的字段不存在");
		}
		if(insertObj==null && updateObj==null) {
			return Result.error("校验的数据不存在");
		}
		if(StringUtils.isEmpty(pk)) {
			return Result.error("主键不能为空");
		}
		if(insertObj==null && updateObj==null){
			return Result.error("数据为空");
		}
		

		List<T> resList = new ArrayList<>();
		if(insertObj!=null) {
			resList = queryList(service, checkObj, checkFields, insertObj, isjoint);
			if(resList.size()==0) {
				return Result.ok();
			}else {
				return Result.error();
			}
		}
		
		if(updateObj!=null) {
			resList = queryList(service, checkObj, checkFields, updateObj, isjoint);
			if(resList.size()==1){
				//判断是否为同一条记录
				try {
					Field f = updateObj.getClass().getDeclaredField(pk);
					f.setAccessible(true);
					if(f.get(updateObj).toString().equals(f.get(resList.get(0)).toString())) {
						return Result.ok();
					}else {
						return Result.error();
					}
				} catch (Exception e) {
					e.printStackTrace();
					throw new ApplicationException(e.getMessage());
				}
			}else if(resList.size()==0){
				return Result.ok();
			}else{
				return Result.error();
			}
		}
	
		
		return Result.ok();
	}
	
	
	private <T> List<T> queryList(IService<T> service,Class<T> checkObj,String[] checkFields,Object obj,boolean isjoint) {
		List<T> resList = new ArrayList<T>();
		QueryWrapper<T> queryWrapper = new QueryWrapper<T>();
		Field field;
		TableField tableField;
		try {
			for(int i=0;i<checkFields.length;i++) {
				field = checkObj.getDeclaredField(checkFields[i]);
				tableField = field.getAnnotation(TableField.class);
				if(tableField!=null) {
					field.setAccessible(true);
					queryWrapper.eq(tableField.value(), QueryUtil.typeConvert(field.getType().toString(), field.get(obj).toString()));
					if(!isjoint && i<checkFields.length-1) {
						queryWrapper.or();
					}
				}else {
					throw new ApplicationException("校验的实体中的字段未配置TableField注解");
				}
			}
			resList = service.list(queryWrapper);
		} catch (Exception e) {
			e.printStackTrace();
			throw new ApplicationException(e.getMessage());
		}
		return resList;
	}
	
//	private <T> T queryOne(IService<T> service,Class<T> checkObj,String[] checkFields,Object obj,boolean isjoint) {
//		QueryWrapper<T> queryWrapper = new QueryWrapper<T>();
//		Field field;
//		TableField tableField;
//		try {
//			for(int i=0;i<checkFields.length;i++) {
//				field = checkObj.getDeclaredField(checkFields[i]);
//				tableField = field.getAnnotation(TableField.class);
//				if(tableField!=null) {
//					field.setAccessible(true);
//					queryWrapper.eq(tableField.value(), QueryUtil.typeConvert(field.getType().toString(), field.get(obj).toString()));
//					if(!isjoint && i<checkFields.length-1) {
//						queryWrapper.or();
//					}
//				}else {
//					throw new ApplicationException("校验的实体中的字段未配置TableField注解");
//				}
//			}
//			return service.getOne(queryWrapper,false);
//		} catch (Exception e) {
//			e.printStackTrace();
//			throw new ApplicationException(e.getMessage());
//		}
//		
//	}
//	
//	
//	private <T> int queryCount(IService<T> service,Class<T> checkObj,String[] checkFields,Object obj,boolean isjoint) {
//		QueryWrapper<T> queryWrapper = new QueryWrapper<T>();
//		Field field;
//		TableField tableField;
//		int count = 0;
//		try {
//			for(int i=0;i<checkFields.length;i++) {
//				field = checkObj.getDeclaredField(checkFields[i]);
//				tableField = field.getAnnotation(TableField.class);
//				if(tableField!=null) {
//					field.setAccessible(true);
//					queryWrapper.eq(tableField.value(), QueryUtil.typeConvert(field.getType().toString(), field.get(obj).toString()));
//					if(!isjoint && i<checkFields.length-1) {
//						queryWrapper.or();
//					}
//				}else {
//					throw new ApplicationException("校验的实体中的字段未配置TableField注解");
//				}
//			}
//			count = service.count(queryWrapper);
//		} catch (Exception e) {
//			e.printStackTrace();
//			throw new ApplicationException(e.getMessage());
//		}
//		return count;
//	}
}
